﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace nhahangbarota
{
    public partial class NhanVien : Form
    {
        DataTable dt = new DataTable();

        public NhanVien()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
            Formchinh a = new Formchinh();
            a.ShowDialog();
        }

        private void NhanVien_Load(object sender, EventArgs e)
        {
            NhanVienBUS nvBUS = new NhanVienBUS();
            dt = nvBUS.LoadDSNV();
            int i = 1;

            gvDSNV.Rows.Clear();
            foreach (DataRow dtRow in dt.Rows)
            {
                DateTime NS = DateTime.Parse(dtRow[3].ToString());
                gvDSNV.Rows.Add(i, dtRow[0].ToString(), dtRow[1].ToString(), dtRow[2].ToString(), dtRow[4].ToString(), NS.ToShortDateString() , dtRow[5].ToString(), dtRow[6].ToString());
                i++;
            }
        }

        private void btnThemNV_Click(object sender, EventArgs e)
        {
            ThemNV f1 = new ThemNV();
            f1.ShowDialog();
            NhanVien_Load(sender, e);
        }

        private void btnXoaNV_Click(object sender, EventArgs e)
        {
            int index = gvDSNV.CurrentRow.Index;

            int maNV = int.Parse(gvDSNV.Rows[index].Cells[1].Value.ToString());

            NhanVienBUS nvBUS = new NhanVienBUS();
            if (nvBUS.XoaNV(maNV) == true)
            {
                MessageBox.Show("Xóa thành công !!");
                NhanVien_Load(sender, e);
            }
        }
        List<NhanVienDTO> ListNV = new List<NhanVienDTO>();
        private void btnCapNhatNV_Click(object sender, EventArgs e)
        {
            int co = 0;
            for (int i = 0; i < ListNV.Count; i++)
            {
                NhanVienBUS nvBUS = new NhanVienBUS();
                if (nvBUS.CapNhat(ListNV[i]) == false)
                    co = 1;
            }

            if (co == 0)
                MessageBox.Show("Cập nhật thành công", "Thông báo");
            NhanVien_Load(sender, e);
        }

        private void gvDSNV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int index = gvDSNV.CurrentRow.Index;

            NhanVienDTO nvDTO = new NhanVienDTO();

            nvDTO.MaNV = int.Parse(gvDSNV.Rows[index].Cells[1].Value.ToString());
            nvDTO.HoTen = gvDSNV.Rows[index].Cells[2].Value.ToString();
            nvDTO.CMND = gvDSNV.Rows[index].Cells[3].Value.ToString();
            nvDTO.GioiTinh = gvDSNV.Rows[index].Cells[4].Value.ToString();
            nvDTO.NgaySinh = DateTime.Parse(gvDSNV.Rows[index].Cells[5].Value.ToString());
            nvDTO.DiaChi = gvDSNV.Rows[index].Cells[6].Value.ToString();
            nvDTO.SDT = gvDSNV.Rows[index].Cells[7].Value.ToString();

            ListNV.Add(nvDTO);
        }
    }
}
